#clean up
rm(list=ls())

#Prior Data Subsetting Commands
#library(krige)
#data(lowerCombined)
#head(lowerCombined)
#fl<-subset(lowerCombined,subset=st=="FL")
#write.csv(fl,"fl2011.csv",row.names=F)

#load data
fl<-read.csv("fl2011.csv")
summary(fl)
table(fl$h7161,fl$party)
democrats<-subset(fl,subset=party=="D" )

#Where do Democrats divide on H. 7161, using Kriging
#postscript("flKriging.eps",width=5,height=5)
plot(y=democrats$np_score,x=democrats$krige.lower,type="n",xlab="District Ideology (Kriging)",ylab="Legislator Ideology (Shor & McCarty)")
points(y=democrats$np_score[democrats$h7161==1],x=democrats$krige.lower[democrats$h7161==1],pch=3,col='black')
points(y=democrats$np_score[democrats$h7161==0],x=democrats$krige.lower[democrats$h7161==0],pch=1,col='red')
abline(h=mean(democrats$np_score),col='gray60')
abline(v=mean(democrats$krige.lower),col='gray60')
legend(x=58,y=-1.75,legend=c("Yea","Nay"),pch=c(3,1),col=c("black","red"))
#dev.off()
#points(y=democrats$np_score[democrats$name=="Waldman, James"],x=democrats$krige.lower[democrats$name=="Waldman, James"],pch=10,col='green')

#Where do Democrats divide on H. 7161, using MRP
#postscript("flMRP.eps",width=5,height=5)
plot(y=democrats$np_score,x=democrats$mrp_mean,type="n",xlab="District Ideology (MRP)",ylab="Legislator Ideology (Shor & McCarty)")
points(y=democrats$np_score[democrats$h7161==1],x=democrats$mrp_mean[democrats$h7161==1],pch=3,col='black')
points(y=democrats$np_score[democrats$h7161==0],x=democrats$mrp_mean[democrats$h7161==0],pch=1,col='red')
abline(h=mean(democrats$np_score),col='gray60')
abline(v=mean(democrats$mrp_mean),col='gray60')
legend(x=-.05,y=-1.75,legend=c("Yea","Nay"),pch=c(3,1),col=c("black","red"))
#dev.off()

#Where do Democrats divide on H. 7161, using Vote Share
democrats$mccain<-1-democrats$pres_2008
#postscript("flPres.eps",width=5,height=5)
plot(y=democrats$np_score,x=democrats$mccain,type="n",xlab="McCain Share of Presidential Vote",ylab="Legislator Ideology (Shor & McCarty)")
points(y=democrats$np_score[democrats$h7161==1],x=democrats$mccain[democrats$h7161==1],pch=3,col='black')
points(y=democrats$np_score[democrats$h7161==0],x=democrats$mccain[democrats$h7161==0],pch=1,col='red')
abline(h=mean(democrats$np_score),col='gray60')
abline(v=mean(democrats$mccain),col='gray60')
legend(x=.5,y=-1.75,legend=c("Yea","Nay"),pch=c(3,1),col=c("black","red"))
#dev.off()


###Comparison with Tausanovich & Warshaw###
#clean up
rm(list=ls())

#library(stringr)
#load("/Users/monogan/Dropbox/KrigingProject/krige/data/lowerCombined.rda")
#lc<-subset(lowerCombined,subset=!st%in%c("NH","VT") & !str_detect(lower,"A")& !str_detect(lower,"B")& !str_detect(lower,"C"),select=c(st,lower,STATEA,krige.lower))
#lc$shd_fips_num<-lc$STATEA*1000+as.numeric(lc$lower)
#tw<-read.csv("shd_2002_TW_ideology_estimates_v2.csv")
#tw<-subset(tw,select=c(shd_fips_num,mrp_mean))
#cross.check<-merge(x=lc,y=tw,by="shd_fips_num",all=F)
#dim(lc);dim(tw);dim(cross.check)
#write.csv(cross.check,"lowerCheck.csv",row.names=F)

#load data, correlation
cross.check<-read.csv("lowerCheck.csv")
head(cross.check)
table(cross.check$st)
cor(cross.check$krige.lower,cross.check$mrp_mean,use="pairwise")#0.6117307


###Comparison with Monogan & Gill###
#clean up
rm(list=ls())

#library(foreign)
#new.data<-read.csv("stateWholeKrigingFine.csv")
#new.data$fips<-new.data$STATEA
#new.data<-subset(new.data,select=c(fips,krige.state,abbr),subset=!abbr%in%c("AK","HI"))
#mg<-read.dta("stateMeasures.dta")
#mg<-subset(mg,select=c(fips,krigestate))
#state.check<-merge(x=new.data,y=mg,by="fips")
#write.csv(state.check,"stateCheck.csv", row.names=F)

#load data, correlation
state.check<-read.csv("stateCheck.csv")
head(state.check)
cor(state.check$krige.state,state.check$krigestate)#0.9892595
